package com.mobis.spit.service;

import com.mobis.spit.dao.SpitDao;
import com.mobis.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.IdWorker;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SpitService {
    @Autowired
    private SpitDao spitDao;

    @Autowired
    private IdWorker idWorker;

    public List<Spit> findAll(){
        return spitDao.findAll();
    }

    public Spit findSpitById(String id){
        return  spitDao.findById(id).get();
    }

    public void addSpid(Spit spit){
        spit.set_id(idWorker.nextId()+"");
        spitDao.save(spit);
    }

    public void update(Spit spit){
        spitDao.save(spit);
    }

    public void deleteBySpit(String id){
        spitDao.deleteById(id);
    }

    public Page<Spit> findByParentId(String id,int page,int size){
        PageRequest request=PageRequest.of(page-1,size);
        return spitDao.findByParentid(id,request);
    }

    public void thumbup(String id){

        Spit spit=spitDao.findById(id).get();
        spit.setThumbup((spit.getThumbup()==null?0:spit.getThumbup())+1);
        spitDao.save(spit);


    }
}
